package servlets;

import dao.JdbcUserDao;
import entries.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class CheckUser extends Dispatcher {

    private final static Log LOGGER = LogFactory.getLog(CheckUser.class);

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String login = req.getParameter("login");
        String password = req.getParameter("password");

        if ( (login == null) || (password == null) || (login == "") || (password == "")){
            req.setAttribute("error", "Invalid login or password");
            this.forward("/login.jsp", req, resp);
            return;
        }
        else{

            User user = null;
            JdbcUserDao userDao = new JdbcUserDao();

            try{
                 user = userDao.findByLogin(login);
            }catch (SQLException e){
                LOGGER.error("SQLException", e);
                throw new RuntimeException(e);
            } catch (ClassNotFoundException e) {
                LOGGER.error("ClassNotFoundException", e);
                throw new RuntimeException(e);
            }

            if (user == null  || !password.equals(user.getPassword())) {
                req.setAttribute("error", "invalid login or password");
                this.forward("/login.jsp", req, resp);
                return;
            }
            else if (user.getRole().getName().equalsIgnoreCase("Admin")){
                    try {
                        HttpSession session = req.getSession();
                        List<User> users = userDao.findAll();
                        session.setAttribute("users", users);
                        req.setAttribute("role", user.getRole().getName());
                        session.setAttribute("lastName", user.getLastName());
                        this.forward("/adminPage.jsp", req, resp);
                        return;
                    } catch (SQLException e) {
                        LOGGER.error("SQLException", e);
                        throw new RuntimeException(e);
                    } catch (ClassNotFoundException e) {
                        LOGGER.error("ClassNotFoundException", e);
                        throw new RuntimeException(e);
                    }
             }
             else if (user.getRole().getName().equalsIgnoreCase("User")){
                     HttpSession session = req.getSession();
                     session.setAttribute("username", user.getFirstName());
                     session.setAttribute("role", user.getRole().getName());
                     this.forward("/helloUser.jsp", req, resp);
                     return;
            }
        }
    }
}
